import { createStore } from 'vuex'
import Works from '../data/Works.json'

export default createStore({
    state: {
        isDarkMode: true,
        showingWorks:[],
        searchInput: '',
    },
    mutations: {
        REFRESH(state, works){
            /* 刷新显示的内容 */
            state.showingWorks = []
            /* 获得随机不重复的作品 */
            for(let i = 0; i < 12; i++){
                let r = Math.floor(Math.random() * works.length) // random => [0,1)
                state.showingWorks.push(works[r])
                works.splice(r, 1)
            }
            
        },
        GETINPUT(state, value){
            state.searchInput = value
        },
        /* 搜索 */
        SEARCH(state, keywd){
            let a = Works.works.filter(function(e){
                return e.title.indexOf(keywd) >= 0
            })
            console.log(a); 
            state.showingWorks = a
        }
    },
    actions: {
        refresh(context){
            context.commit('REFRESH', [].concat(Works.works))
        },
        getInput(context, value){
            context.commit('GETINPUT', value)
        },
        search(content, keywd){
            content.commit('SEARCH', keywd)
        }
    },
})
